package com.droowa.util;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.view.document.AbstractExcelView;

import com.droowa.domain.Customer;
import com.droowa.domain.Product;

@Component
public class CustomerView  extends AbstractExcelView{

	@SuppressWarnings("unchecked")
	@Override
	protected void buildExcelDocument(Map<String, Object> model,
			HSSFWorkbook workbook, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		response.setHeader("Content-Disposition", "attachment; filename=customer.xls");
		HSSFSheet sheet = createFirstSheet(workbook);
		createColumnLabel(sheet);
		
		List<Customer> customer = (List<Customer>)model.get("customer");
		int rowNum=1;
		for(Customer cus : customer){
			createShopManagerRow(sheet, cus, rowNum++);
		}
	}

	private HSSFSheet createFirstSheet(HSSFWorkbook workbook) {
		HSSFSheet sheet = workbook.createSheet();
		workbook.setSheetName(0, "회원정보");//0번 sheet에 업무기록이라는 이름 부여
		sheet.setColumnWidth((short)1, (short)(3000));
		sheet.setColumnWidth((short)2, (short)(3300));
		sheet.setColumnWidth((short)3, (short)(5500));
		sheet.setColumnWidth((short)4, (short)(4000));
		sheet.setColumnWidth((short)5, (short)(10500));
		sheet.setColumnWidth((short)6, (short)(3300));
		return sheet;
	}
	
	private void createColumnLabel(HSSFSheet sheet) {
		HSSFRow firstRow = sheet.createRow(0);
		HSSFCell cell = firstRow.createCell((short) 0);
		cell.setCellValue("ID");
		
		cell = firstRow.createCell((short) 1);
		cell.setCellValue("이름");
		
		cell = firstRow.createCell((short) 2);
		cell.setCellValue("생년월일");		
		
		cell = firstRow.createCell((short) 3);
		cell.setCellValue("Email");		
		
		cell = firstRow.createCell((short) 4);
		cell.setCellValue("Phone");
		
		cell = firstRow.createCell((short) 5);
		cell.setCellValue("주소");

		cell = firstRow.createCell((short) 6);
		cell.setCellValue("가입일");
		
	}
	
	private void createShopManagerRow(HSSFSheet sheet, Customer customer, int rowNum) {
		HSSFRow row = sheet.createRow(rowNum);
		HSSFCell cell = row.createCell((short)0);
		cell.setCellValue(customer.getCustId());
		
		cell = row.createCell((short)1);
		cell.setCellValue(customer.getName());
		
		cell = row.createCell((short)2);
		cell.setCellValue(customer.getBirth());
		
		cell = row.createCell((short)3);
		cell.setCellValue(customer.getEmail());
		
		cell = row.createCell((short)4);
		cell.setCellValue(customer.getPhone());
		
		cell = row.createCell((short)5);
		cell.setCellValue(customer.getAddress());
		
		cell = row.createCell((short)6);
		cell.setCellValue(customer.getJoinDate());
		
	}
		
}
